package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.common.ObjectUtil;
import com.shop.common.result.Result;
import com.shop.common.result.util.ResultModel;
import com.shop.dto.factory.CommodityColorDto;
import com.shop.dto.factory.SelectFactoryCommodityCommodityAndFactoryByIdDto;
import com.shop.entity.*;
import com.shop.mapper.*;
import com.shop.service.FactoryCommodityExtendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.vo.factory.SelectFactoryCommodityCommodityAndFactoryByIdVo;
import com.shop.vo.factory.SelectFactoryCommodityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 卖家商品扩展表 服务实现类
 * </p>
 *
 * @author 靳 旺
 * @since 2021-03-17
 */
@Service
public class FactoryCommodityExtendServiceImpl extends ServiceImpl<FactoryCommodityExtendDoMapper, FactoryCommodityExtendDo> implements FactoryCommodityExtendService {


    @Autowired
    private CommodityDoMapper commodityDoMapper;

    @Autowired
    private FactoryDoMapper factoryDoMapper;

    @Autowired
    private ProductPicDoMapper productPicDoMapper;

    @Autowired
    private WarehouseInfoDoMapper warehouseInfoDoMapper;

    @Autowired
    private CommodityWarehouseRelationDoMapper commodityWarehouseRelationDoMapper;

    @Autowired
    private CommodityColorRelationDoMapper commodityColorRelationDoMapper;

    @Autowired
    private CommodityColorDoMapper commodityColorDoMapper;

    @Autowired
    private CommoditySizeDoMapper commoditySizeDoMapper;

    @Autowired
    private CommoditySizeRelationDoMapper commoditySizeRelationDoMapper;

    @Override
    public ResultModel selectFactoryCommodityCommodityAndFactoryById(SelectFactoryCommodityCommodityAndFactoryByIdVo selectFactoryCommodityCommodityAndFactoryByIdVo) {

        /* 1、校验参数 */
        Assert.notNull(selectFactoryCommodityCommodityAndFactoryByIdVo.getPkFactoryCommodityExtendId(), "商品id不能为空！");

        /* 2、获取厂家商品扩展信息 */
        FactoryCommodityExtendDo factoryCommodityExtend = this.selectFactoryCommodity(selectFactoryCommodityCommodityAndFactoryByIdVo.getPkFactoryCommodityExtendId());

        /*  3、获取商品基础参数 */
        CommodityDo commodityDo = this.selectCommodity(factoryCommodityExtend.getFkCommodityId());

        /*  4、获取厂家信息 */
        FactoryDo factoryDo = this.selectFactory(factoryCommodityExtend.getFkFactoryId());

        /* 5、获取图片信息 */
        List<ProductPicDo> productPics = this.selectProductPic(commodityDo.getPkCommodityId());

        /* 6、获取仓库信息 */
        WarehouseInfoDo warehouseInfoDo = this.selectWarehose(commodityDo.getPkCommodityId());

        /* 7、获取商品颜色 */
        List<CommodityColorDto> commodityColorDtos = this.selectCommodityColorRelation(commodityDo.getPkCommodityId());

        List<CommoditySizeDo> commoditySizeDos = this.selectCommoditySize(commodityDo);

        /* 8、设置返回参数 */
        SelectFactoryCommodityCommodityAndFactoryByIdDto selectFactoryCommodityCommodityAndFactoryByIdDto = this.setUpReturnParam(factoryCommodityExtend, commodityDo, factoryDo, productPics, warehouseInfoDo, commodityColorDtos,commoditySizeDos);

        return Result.successResultModel(selectFactoryCommodityCommodityAndFactoryByIdDto);
    }

    private SelectFactoryCommodityCommodityAndFactoryByIdDto setUpReturnParam(FactoryCommodityExtendDo factoryCommodityExtend, CommodityDo commodityDo, FactoryDo factoryDo, List<ProductPicDo> productPics, WarehouseInfoDo warehouseInfoDo, List<CommodityColorDto> commodityColorDtos, List<CommoditySizeDo> commoditySizeDos) {
        SelectFactoryCommodityCommodityAndFactoryByIdDto selectFactoryCommodityCommodityAndFactoryByIdDto = new SelectFactoryCommodityCommodityAndFactoryByIdDto();
        selectFactoryCommodityCommodityAndFactoryByIdDto.setCommodityDo(commodityDo);
        selectFactoryCommodityCommodityAndFactoryByIdDto.setFactoryCommodityExtendDo(factoryCommodityExtend);
        selectFactoryCommodityCommodityAndFactoryByIdDto.setFactoryDo(factoryDo);
        selectFactoryCommodityCommodityAndFactoryByIdDto.setProductPics(productPics);
        selectFactoryCommodityCommodityAndFactoryByIdDto.setWarehouseInfoDo(warehouseInfoDo);
        selectFactoryCommodityCommodityAndFactoryByIdDto.setCommodityColorDtos(commodityColorDtos);
        selectFactoryCommodityCommodityAndFactoryByIdDto.setCommoditySizeDos(commoditySizeDos);
        return selectFactoryCommodityCommodityAndFactoryByIdDto;
    }


    private List<CommoditySizeDo> selectCommoditySize(CommodityDo commodityDo) {
        QueryWrapper<CommoditySizeRelationDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fk_commodity_id", commodityDo.getPkCommodityId());
        List<CommoditySizeRelationDo> commoditySizeRelationDos = commoditySizeRelationDoMapper.selectList(queryWrapper);
        if (commoditySizeRelationDos.size() <= 0) return null;
        List<CommoditySizeDo> commoditySizeDos = commoditySizeDoMapper.selectBatchIds(commoditySizeRelationDos.stream().map(CommoditySizeRelationDo::getFkCommoditySizeId).collect(Collectors.toList()));
        commoditySizeDos.sort((v,k) -> v.getSizeSort().compareTo(k.getSizeSort()));
        return commoditySizeDos;
    }


    /**
     * 根据id 查询厂家商品扩展信息
     *
     * @param id
     * @return FactoryCommodityExtendDo
     */
    private FactoryCommodityExtendDo selectFactoryCommodity(Long id) {
        QueryWrapper<FactoryCommodityExtendDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pk_factory_commodity_extend_id", id);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据id获取商品基础参数
     *
     * @param id
     * @return CommodityDo
     */
    private CommodityDo selectCommodity(Long id) {
        QueryWrapper<CommodityDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pk_commodity_id", id);
        return commodityDoMapper.selectOne(queryWrapper);
    }

    /**
     * 获取厂家信息
     *
     * @param id
     * @return
     */
    private FactoryDo selectFactory(Long id) {
        QueryWrapper<FactoryDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pk_factory_id", id);
        return factoryDoMapper.selectOne(queryWrapper);
    }

    /**
     * 获取图片信息
     *
     * @param id
     * @return
     */
    private List<ProductPicDo> selectProductPic(Long id) {
        QueryWrapper<ProductPicDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fk_commodity_id", id);
        return productPicDoMapper.selectList(queryWrapper);
    }


    /**
     * '
     * 查询仓库信息
     *
     * @param commodityId
     * @return
     */
    private WarehouseInfoDo selectWarehose(Long commodityId) {
        QueryWrapper<CommodityWarehouseRelationDo> commodityWarehouseRelationDoQueryWrapper = new QueryWrapper<>();
        commodityWarehouseRelationDoQueryWrapper.eq("fk_commodity_id", commodityId);
        CommodityWarehouseRelationDo commodityWarehouseRelationDos = commodityWarehouseRelationDoMapper.selectOne(commodityWarehouseRelationDoQueryWrapper);
       if (commodityWarehouseRelationDos == null) return  null;
        QueryWrapper<WarehouseInfoDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pk_warehouse_info_id", commodityWarehouseRelationDos.getFkWarehouseInfoId());
        return warehouseInfoDoMapper.selectOne(queryWrapper);
    }

    /**
     * 获取商品颜色
     *
     * @param commodityId
     * @return
     */
    private List<CommodityColorDto> selectCommodityColorRelation(Long commodityId) {
        QueryWrapper<CommodityColorRelationDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fk_commodity_id", commodityId);
        List<CommodityColorRelationDo> commodityColorRelationDos = commodityColorRelationDoMapper.selectList(queryWrapper);
        if (commodityColorRelationDos == null) return  null;
        if (commodityColorRelationDos.size() <= 0) return null;
        List<Long> colorId = commodityColorRelationDos.stream().map(CommodityColorRelationDo::getFkCommodityColorId).collect(Collectors.toList());
        Map<Long, List<CommodityColorDo>> commodityColorDos = commodityColorDoMapper.selectBatchIds(colorId).stream().collect(Collectors.groupingBy(CommodityColorDo::getPkCommodityColorId));
        List<CommodityColorDto> commodityColorDtos = new ArrayList<>();
        commodityColorRelationDos.forEach(k -> {
            CommodityColorDto commodityColorDto = new CommodityColorDto();
            ObjectUtil.annotationToObject(k, commodityColorDto);
            ObjectUtil.annotationToObject(commodityColorDos.get(k.getFkCommodityColorId()).get(0), commodityColorDto);
            commodityColorDtos.add(commodityColorDto);
        });
        return commodityColorDtos;
    }

}
